package knowledge

import (
	"context"

	"gitee.com/linxing_3/sr-core/config/source"
	"gitee.com/linxing_3/sr-core/domain"
	"gitee.com/mkwchecking/sr-admin/apps/knowledge/adapter/http"
	"gitee.com/mkwchecking/sr-admin/apps/knowledge/conf"
	"gitee.com/mkwchecking/sr-admin/apps/knowledge/entity"

	"gitee.com/mkwchecking/sr-admin/pkg/plugins/pdatabase"
)

var _ domain.IDomain = (*KnowledgeDomain)(nil)

func NewKnowledgeDomain() domain.IDomain {
	return &KnowledgeDomain{}
}

type KnowledgeDomain struct {
}

// Install implements domain.IDomain.
func (a *KnowledgeDomain) Install(ctx context.Context, src source.ISource) error {
	var err error
	conf.SetupConfig(src)

	if err = http.InitHttp(ctx); err != nil {
		return err
	}

	// ollama.Instance(
	// 	ollama.WithBin(conf.Conf.Ollama.BinPath),
	// 	ollama.WithUrl(conf.Conf.Ollama.Addr),
	// 	ollama.WithCacheFilePath(conf.Conf.Ollama.CacheDir),
	// )

	return err
}

// OnSourceChange implements domain.IDomain.
func (a *KnowledgeDomain) BeforeStart(src source.ISource) error {

	if err := entity.AutoMigrate(pdatabase.MustGetDatabase()); err != nil {
		return err
	}

	return nil
}

// Start implements domain.IDomain.
func (a *KnowledgeDomain) Start(ctx context.Context) error {
	/* return http.OnStart(ctx) */
	// if ollCl, _ := ollama.Instance(); ollCl != nil {
	// 	return ollCl.Start(ctx)
	// }
	return nil
}

// Stop implements domain.IDomain.
func (a *KnowledgeDomain) Stop(ctx context.Context) error {
	return nil
}
